package com.example.piece.ui.demo

import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Route
import com.dds.fingerprintidentify.FingerprintIdentify
import com.dds.fingerprintidentify.base.BaseFingerprint
import com.example.base.aop.click.RepeatClick
import com.example.base.ext.navigationTo
import com.example.base.ext.toast
import com.example.base.tools.JsonTool
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.TestActivityFingerBinding
import com.example.piece.tools.GestureUnlock
import com.example.piece.ui.activity.Layer3Activity
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.dialogs.WaitDialog
import com.wenming.library.util.LogUtil

@Route(path = RoutePath.TESTActivity.FINGER_TEST_ACTIVITY)
class FingerTestActivity : Layer3Activity<TestActivityFingerBinding, ViewDataBinding>() {
    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = false
    override val leftIcon1Res: Int = R.drawable.ic_back
    override val centerLayoutRes: Int = R.layout.test_activity_finger
    var mFingerprintIdentify: FingerprintIdentify? = null

    override val title: String
        get() = "指纹及图形解锁"

    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }


    //初始化指纹
    @RepeatClick
    fun onInitFinger(view: View) {

        mFingerprintIdentify = FingerprintIdentify(this,
            object : BaseFingerprint.FingerprintIdentifyExceptionListener {
                override fun onCatchException(exception: Throwable?) {
                    toast("指纹验证初始化异常：" + exception?.message)
                }

            })

        if (mFingerprintIdentify?.isHardwareEnable == false) {
            toast("指纹验证初始化失败：设备硬件不支持指纹识别")

        }
        if (mFingerprintIdentify?.isRegisteredFingerprint == false) {
            toast("指纹验证初始化失败：未注册指纹")
            return
        }

        if (mFingerprintIdentify?.isFingerprintEnable == false) {
            toast("指纹验证初始化失败：指纹识别不可用")
            return
        }
        toast("指纹验证初始化正常")
    }

    //开启指纹验证
    @RepeatClick
    fun onStartFinger(view: View) {
        onInitFinger(view)
        WaitDialog.show("指纹验证不通过，还可以尝试"+3+"次",WaitDialog.TYPE.NONE)

        mFingerprintIdentify?.startIdentify(3,
            object : BaseFingerprint.FingerprintIdentifyListener {
                override fun onSucceed() {
                    WaitDialog.show("指纹验证通过",WaitDialog.TYPE.SUCCESS)
                    //toast("指纹验证通过")

                    LogUtil.e("指纹验证通过")
                }

                override fun onNotMatch(availableTimes: Int) {
                    WaitDialog.show("指纹验证不通过，还可以尝试"+availableTimes+"次",WaitDialog.TYPE.NONE)
                    LogUtil.e("指纹验证不通过，还可以尝试"+availableTimes+"次")

                   // toast("指纹验证不通过")
                }

                override fun onFailed() {
                  //  toast("指纹验证错误次数超过上限")
                    WaitDialog.show("指纹验证不通过",WaitDialog.TYPE.ERROR)
                    LogUtil.e("指纹验证不通过")

                    mFingerprintIdentify?.cancelIdentify()

                }
            })

    }

    //设置图形
    @RepeatClick
    fun onSettingGesture(view: View) {
        GestureUnlock.getInstance().init(applicationContext)
        navigationTo(
            RoutePath.TESTActivity.GESTURE_TEST_ACTIVITY,
            mutableMapOf(
                Key.DataKey.H5_TITLE to "设置解锁图形",
                Key.DataKey.GESTURE_TYPE to 0))
    }

    //修改图形
    @RepeatClick
    fun onFixGesture(view: View) {
        GestureUnlock.getInstance().init(applicationContext)
        navigationTo(
            RoutePath.TESTActivity.GESTURE_TEST_ACTIVITY,
            mutableMapOf(
                Key.DataKey.H5_TITLE to "修改解锁图形",
                Key.DataKey.GESTURE_TYPE to 1))
    }

    //验证
    @RepeatClick
    fun onCheckGesture(view: View) {
        GestureUnlock.getInstance().init(applicationContext)
        navigationTo(
            RoutePath.TESTActivity.GESTURE_TEST_ACTIVITY,
            mutableMapOf(
                Key.DataKey.H5_TITLE to "校验解锁图形",
                Key.DataKey.GESTURE_TYPE to 2))
    }

    override fun onPause() {
        super.onPause()
        mFingerprintIdentify?.cancelIdentify()
    }

    override fun onPostResume() {
        super.onPostResume()
        mFingerprintIdentify?.resumeIdentify()

    }
}